var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var Pagination = require('../../utils/pagination.js');
var common = require('../../utils/common.js');
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置

Page({
	data: {
		dataLoaded: false,
		goodsList: [],
		articleList: [],

		tabs: ["商品", "文章"],
		activeIndex: 0,
		sliderOffset: 0,
		sliderLeft: 0,
		sliderWidth,

		page1: {
			nextPageLoading: false,
			pageLoadedAll: false,
		},

		page2: {
			nextPageLoading: false,
			pageLoadedAll: false,
		},

	},

	onLoad() {
		this._initTabs();
		this._getGoodsList();
		this._getArticleList();
	},

	onReachBottom() {
		var _self = this;
		var num = +_self.data.activeIndex + 1;
		var pagination;

		if (_self.data['page' + num].pageLoadedAll) {
			return;
		}

		pagination = _self['pagination' + num];

		pagination && pagination.loadNext();
	},

	_initTabs() {
		var that = this;

		wx.getSystemInfo({
			success: function(res) {
				that.setData({
					sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
					sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
				});
			}
		});
	},

	tabClick: function(e) {
		var id = e.currentTarget.id;
		var formId= e.detail.formId;

		this.setData({
			sliderOffset: e.currentTarget.offsetLeft,
			activeIndex: id
		});
		common.submitForm(formId);
	},

	_itemClick(evt) {
		var data = evt.currentTarget.dataset;

		if (data.state == 0) {
			util.showOK((data.type == 0 ? '商品' : '文章') + '已下架');
			return;
		}

		common.jump(evt);

	},


	//已收藏商品列表
	_getGoodsList() {
		var _self = this;

		_self.pagination1 = new Pagination({
			url: '/openapi/CUser/myCollections',
			params: {
				type: 0,
			},
			beforeGetPage(page) {
				var page1 = _self.data.page1;

				if (page > 1) {
					page1.nextPageLoading = true;
					_self.setData({
						page1
					});
				}
			},
			getPageSuccess(page, data) {
				var result = data.result;
				var page1 = _self.data.page1;
				var goodsList = _self.data.goodsList || [];
				var curGoodsList = result['collectList'] || [];
				var data = {};

				if (page == 1) {
					data = {
						dataLoaded: true,
					};
				}

				if (curGoodsList.length < 10) {
					page1.pageLoadedAll = true;
				}

				page1.nextPageLoading = false;

				data.page1 = page1;
				data.goodsList = goodsList.concat(curGoodsList);

				_self.setData(data);
			}
		});
	},

	//已收藏文章列表
	_getArticleList() {
		var _self = this;

		_self.pagination2 = new Pagination({
			url: '/openapi/CUser/myCollections',
			params: {
				type: 1,
			},
			beforeGetPage(page) {
				var page2 = _self.data.page2;

				if (page > 1) {
					page2.nextPageLoading = true;
					_self.setData({
						page2
					});
				}
			},
			getPageSuccess(page, data) {
				var result = data.result;
				var page2 = _self.data.page2;
				var articleList = _self.data.articleList || [];
				var curArticleList = result['collectList'] || [];
				var data = {};

				if (curArticleList.length < 10) {
					page2.pageLoadedAll = true;
				}

				page2.nextPageLoading = false;
				data.page2 = page2;

				data.articleList = articleList.concat(curArticleList);

				_self.setData(data);
			}
		});
	},

	//取消收藏
	_delCollect(evt) {
		var _self = this;
		var collectId = evt.currentTarget.dataset.collectId;

		ajax.P('/openapi/CUser/delCollections', {
			idStr: collectId + ''
		}, function(data) {
			var key = _self.data.activeIndex == 0 ? 'goodsList' : 'articleList';
			var list = _self.data[key];

			list.some(function(item, index) {
				if (item.id == collectId) {
					list.splice(index, 1);
					return true;
				}
			});

			_self.setData({
				[`${key}`]: list
			});

			util.showOK('已取消收藏');

		});
	}

});